<?php

namespace api\modules\v1\controllers\statistics;

use services\baibai\UpdateDataMigrateService;
use Yii;
use common\models\statistics\ManageReport;
use api\controllers\OnAuthController;
use yii\web\HttpException;
use Exception;

class ManagementReportController extends OnAuthController
{
    /**
     * @var string
     */
    public $modelClass = '';


    /**
     * @api {get} /v1/statistics/management-report 获取经营报表
     * @apiName management-report
     * @apiGroup statistics
     *
     * @apiParam {Number} type                                             维度类型， 1：日， 2：月， 3：年
     * @apiParam {String} start_time                                       开始时间【格式：2020-01-01】
     * @apiParam {String} end_time                                         结束时间【格式：2020-01-01】
     * @apiParam {Number} page                                             页码
     * @apiParam {Number} pageSize                                         每页显示数
     *
     * @apiSuccess {Object}  data                                          返回数据
     * @apiSuccess {Number}  data.page                                     当前页码
     * @apiSuccess {Number}  data.pageSize                                 每页显示数
     * @apiSuccess {Number}  data.total                                    总数
     * @apiSuccess {Array}   data.list                                     列表
     * @apiSuccess {Number}  data.list.id                                  ID
     * @apiSuccess {Number}  data.list.project_id                          项目ID
     * @apiSuccess {Number}  data.list.pid
     * @apiSuccess {String}  data.list.time                                时间
     * @apiSuccess {Number}  data.list.type                                回访内容
     * @apiSuccess {Number}  data.list.clinic_id                           操作人ID
     * @apiSuccess {String}  data.list.clinic_name                         操作人姓名
     * @apiSuccess {Number}  data.list.reg_num                             总挂号
     * @apiSuccess {Number}  data.list.advisory_num                        咨询号
     * @apiSuccess {Number}   data.list.loss_num                           流失
     * @apiSuccess {String}   data.list.loss_rate                          流失率
     * @apiSuccess {Number}   data.list.initially_num                      初诊
     * @apiSuccess {Number}   data.list.referral_num                       复诊
     * @apiSuccess {Number}   data.list.last_month_treat_num               上月就诊数
     * @apiSuccess {String}   data.list.diagnosis_rate                     断诊率
     * @apiSuccess {Number}   data.list.introduction_num                   介绍号
     * @apiSuccess {array}   data.list.children                            子集
     *
     * @author wangxiang
     * @return array
     * @throws HttpException
     */
    public function actionIndex()
    {
        $projectId = $this->projectId;
        $startTime = (string)Yii::$app->request->get('start_time', '');
        $endTime = (string)Yii::$app->request->get('end_time', '');
        $type = (int)Yii::$app->request->get('type');
        $page = (int)Yii::$app->request->get('page', 1);
        $pageSize = (int)Yii::$app->request->get('pageSize', 15);

        if (!$startTime || !$endTime) {
            throw new HttpException(422, '开始时间和结束时间不能为空！');
        }

        // 根据type来定义时间
        if ($type == ManageReport::TYPE_DAY) {
            $startTime = strtotime($startTime);
            $endTime = strtotime($endTime);
        } else if ($type == ManageReport::TYPE_MONTH) {
            $startTime = strtotime(date('Y-m-01', strtotime($startTime)));
            $endTime = strtotime(date('Y-m-01', strtotime($endTime)));
        } else if ($type == ManageReport::TYPE_YEAR) {
            $startTime = strtotime(date('Y-01-01', strtotime($startTime)));
            $endTime = strtotime(date('Y-01-01', strtotime($endTime)));
        }

        if (!in_array($type, [ManageReport::TYPE_DAY, ManageReport::TYPE_MONTH, ManageReport::TYPE_YEAR])) {
            throw new HttpException(422, 'type类型不正确！');
        }

        $params = [
            'project_id' => $projectId,
            'start_time' => $startTime,
            'end_time' => $endTime,
            'type' => $type,
        ];

        try {
            $data = Yii::$app->services->statisticsManagementReport->getData($params, $page, $pageSize);
        } catch (Exception $exception) {
            throw new HttpException(500, $exception->getMessage());
        }

        return $data;
    }

    /**
     * @api {get} /v1/statistics/management-report/perfection-list 完善度报表
     * @apiGroup 分组名称
     * @apiName 完善度报表
     * @apiVersion 1.0.0
     *
     * @apiDescription 完善度报表
     *
     * @apiParam {String} month_is  月份【2020-06】
     *
     * @apiSuccess {Number} code    状态码，0：请求成功
     * @apiSuccess {String} message   提示信息
     * @apiSuccess {Object} data    返回数据
     *
     */
    public function actionPerfectionList()
    {
        $monthIs = Yii::$app->request->get('month_is');
        return Yii::$app->services->perfectionReport->getPerfection($this->projectId, strtotime($monthIs));
    }

    /**
     * @api {get} /v1/statistics/management-report/perfection-all-list 完善度（项目）报表
     * @apiGroup 分组名称
     * @apiName 完善度（项目）报表
     * @apiVersion 1.0.0
     *
     * @apiDescription 完善度（项目）报表
     *
     * @apiParam {String} month_is  月份【2020-06】
     *
     * @apiSuccess {Number} code    状态码，0：请求成功
     * @apiSuccess {String} message   提示信息
     * @apiSuccess {Object} data    返回数据
     *
     */
    public function actionPerfectionAllList()
    {
        $monthIs = Yii::$app->request->get('month_is');
        return Yii::$app->services->perfectionReport->getPerfectionAll(strtotime($monthIs));
    }

}